/*
* Copyright (C) 2019 Tianjin KYLIN Information Technology Co., Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/&gt;.
*
*/

#include "widget.h"
#include <ukui-log4qt.h>
#include "mostgrandwidget.h"
#include "qtsingleapplication.h"
#include "ukuistylehelper/ukuistylehelper.h"
#include "windowmanager/windowmanager.h"
void qtVersionHandle()
{
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
    QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
#endif

#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
    QApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
#endif
}

int main(int argc, char *argv[])
{
    qtVersionHandle();
    initUkuiLog4qt("ukui-sidebar");

    /* 如果系统中有实例在运行则退出 */
    QString id = QString("ukui-sidebar-" + QLatin1String(getenv("DISPLAY")));
    QtSingleApplication a(id, argc, argv);
    if (a.isRunning()) {
        a.sendMessage(QApplication::arguments().length() > 1 ? QApplication::arguments().at(1) : a.applicationFilePath());
        qDebug() << QObject::tr("ukui-sidebar is already running!");
        return EXIT_SUCCESS;

    } else {
        QCommandLineParser parser;
        QCommandLineOption sidebarState({"s", "state"}, QObject::tr("show or hide sidebar widget"));
        QCommandLineOption sidebarShow({"s", "show"}, QObject::tr("show sidebar widget"));
        QCommandLineOption sidebarHide({"h", "hide"}, QObject::tr("hide sidebar widget"));

        parser.addOptions({sidebarState,sidebarShow,sidebarHide});
        parser.process(a);

        QApplication::setQuitOnLastWindowClosed(false);

        Widget *w = new Widget;
        w->setObjectName("SidebarWidget");
        w->setAttribute(Qt::WA_TranslucentBackground);

        mostGrandWidget::getInstance()->addWidget(w);
        QString platform = QGuiApplication::platformName();
        if(!platform.startsWith(QLatin1String("wayland"),Qt::CaseInsensitive))
        {
            mostGrandWidget::getInstance()->move(-500, 0);
        } else {
            kdk::WindowManager::setGeometry(mostGrandWidget::getInstance()->windowHandle(),QRect(-500,0,0,0));
        }
        mostGrandWidget::getInstance()->setProperty("useSystemStyleBlur", true);
        mostGrandWidget::getInstance()->setVisible(false);

        QTimer *startTimer = new QTimer;
        startTimer->setSingleShot(true);

        QObject::connect(w,&Widget::startRun,[=](QSystemTrayIcon::ActivationReason reason){
            qDebug()<<"Active start";
            startTimer->stop();
            w->startBackgroundFunction();
            QTimer *startTimer1 = new QTimer;
            startTimer1->setSingleShot(true);
            QObject::connect(startTimer1,&QTimer::timeout,[=](){
                w->iconActivated(reason);
            });
            startTimer1->start(1);
        });

        QObject::connect(startTimer,&QTimer::timeout,[=](){
            qDebug()<<"Auto start";
            w->startBackgroundFunction();
            w->setoneShotBool(true);
        });
        startTimer->start(5000);

        QObject::connect(&a, SIGNAL(messageReceived(const QString&)),w, SLOT(bootOptionsFilter(const QString&)));
        return a.exec();
    }
}
